//package com.siecom.hostservice;
//
//import android.bluetooth.BluetoothAdapter;
//import android.bluetooth.BluetoothDevice;
//import android.bluetooth.BluetoothGatt;
//import android.bluetooth.BluetoothGattCallback;
//import android.bluetooth.BluetoothGattCharacteristic;
//import android.bluetooth.BluetoothGattDescriptor;
//import android.bluetooth.BluetoothGattServer;
//import android.bluetooth.BluetoothGattServerCallback;
//import android.bluetooth.BluetoothGattService;
//import android.bluetooth.BluetoothManager;
//import android.bluetooth.le.AdvertiseCallback;
//import android.bluetooth.le.AdvertiseData;
//import android.bluetooth.le.AdvertiseSettings;
//import android.bluetooth.le.BluetoothLeAdvertiser;
//import android.content.Context;
//import android.os.ParcelUuid;
//import android.os.SystemClock;
//import android.util.Log;
//
//import com.siecom.proto.Message;
//import com.siecom.tool.ByteTool;
//import com.siecom.tool.LogUtil;
//import com.siecom.transport.CommServer;
//
//import java.util.Arrays;
//import java.util.UUID;
//
//
//public class BLEperipheral extends CommServer {
//
//    private static final String TAG = BLEperipheral.class.getSimpleName();
//    private Context context;
//
//    private BLEperipheral (){
//        return;
//    }
//    private BLEperipheral(Context context) {
//        this.context = context;
//    }
//
////    public static final UUID UUID_SERVICE = UUID.fromString("49535343-FE7D-4AE5-8FA9-9FAFD205E455"); //自定义UUID
////    public static final UUID UUID_CHAR_READ_NOTIFY = UUID.fromString("49535343-8841-43F4-A8D4-ECBE34729BB3");
////    public static final UUID UUID_DESC_NOTITY = UUID.fromString("49535343-8841-43F4-A8D4-ECBE34729BB3");
////    public static final UUID UUID_CHAR_WRITE = UUID.fromString("49535343-1E4D-4BD9-BA61-23C647249616");
//
//    public static final UUID UUID_SERVICE = UUID.fromString("10000000-0000-0000-0000-000000000000"); //自定义UUID
//    public static final UUID UUID_CHAR_READ_NOTIFY = UUID.fromString("11000000-0000-0000-0000-000000000000");
//    public static final UUID UUID_DESC_NOTITY = UUID.fromString("11100000-0000-0000-0000-000000000000");
//    public static final UUID UUID_CHAR_WRITE = UUID.fromString("12000000-0000-0000-0000-000000000000");
//    private BluetoothLeAdvertiser mBluetoothLeAdvertiser; // BLE广播
//    private BluetoothGattServer mBluetoothGattServer; // BLE服务端
//    private static   BLEperipheral mBlePeripheral = null;
//    private BluetoothGatt mBluetoothGatt = null;
//    private static Object lock = new Object();
//    public static BLEperipheral getBleInstance(Context context){
//        if(mBlePeripheral == null){
//            synchronized (lock){
//                if (mBlePeripheral == null){
//                    mBlePeripheral = new BLEperipheral(context);
//                }
//            }
//        }
//        return mBlePeripheral;
//    }
//
//    public void connect(BluetoothDevice device){
//        String name = device.getName();
//        String address = device.getName();
//        BluetoothDevice  bluetoothDevice  = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(address);
//        mBluetoothGatt = bluetoothDevice.connectGatt(context,true,mBluetoothGattCallback);
//
//    }
//
//    private BluetoothGattCallback mBluetoothGattCallback = new BluetoothGattCallback() {
//        @Override
//        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
//            super.onConnectionStateChange(gatt, status, newState);
//        }
//
//        @Override
//        public void onServicesDiscovered(BluetoothGatt gatt, int status) {
//            super.onServicesDiscovered(gatt, status);
//        }
//
//        @Override
//        public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
//            super.onCharacteristicRead(gatt, characteristic, status);
//        }
//
//        @Override
//        public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
//            super.onCharacteristicWrite(gatt, characteristic, status);
//        }
//
//        @Override
//        public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
//            super.onCharacteristicChanged(gatt, characteristic);
//        }
//
//        @Override
//        public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
//            super.onDescriptorRead(gatt, descriptor, status);
//        }
//
//        @Override
//        public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
//            super.onDescriptorWrite(gatt, descriptor, status);
//        }
//    };
//    public void initBle() {
//        BluetoothManager bluetoothManager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
//        // BluetoothAdapter bluetoothAdapter = bluetoothManager.getAdapter();
//        BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
//        LogUtil.e(TAG,"bluetoothAdapter : " + (bluetoothAdapter == null));
//        // ============启动BLE蓝牙广播(广告) =================================================================================
//        //广播设置(必须)
//        AdvertiseSettings settings = new AdvertiseSettings.Builder()
//                .setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_LOW_LATENCY) //广播模式: 低功耗,平衡,低延迟
//                .setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_HIGH) //发射功率级别: 极低,低,中,高
//                .setConnectable(true) //能否连接,广播分为可连接广播和不可连接广播
//                .build();
//        //广播数据(必须，广播启动就会发送)
//        AdvertiseData advertiseData = new AdvertiseData.Builder()
//                .setIncludeDeviceName(true) //包含蓝牙名称
//                .setIncludeTxPowerLevel(true) //包含发射功率级别
//                .addManufacturerData(1, new byte[]{23, 33}) //设备厂商数据，自定义
//                .build();
//        //扫描响应数据(可选，当客户端扫描时才发送)
//        AdvertiseData scanResponse = new AdvertiseData.Builder()
//                .addManufacturerData(2, new byte[]{66, 66}) //设备厂商数据，自定义
//                .addServiceUuid(new ParcelUuid(UUID_SERVICE)) //服务UUID
//                //      .addServiceData(new ParcelUuid(UUID_SERVICE), new byte[]{2}) //服务数据，自定义
//                .build();
//        LogUtil.e(TAG,"bluetoothAdapter.isEnabled() : " + bluetoothAdapter.isEnabled());
//
//        assert bluetoothAdapter != null;
//        mBluetoothLeAdvertiser = bluetoothAdapter.getBluetoothLeAdvertiser();
//        LogUtil.e(TAG,"mBluetoothLeAdvertiser : " + (mBluetoothLeAdvertiser == null));
//        mBluetoothLeAdvertiser.startAdvertising(settings, advertiseData, scanResponse, mAdvertiseCallback);
//
//        // 注意：必须要开启可连接的BLE广播，其它设备才能发现并连接BLE服务端!
//        // =============启动BLE蓝牙服务端=====================================================================================
//        BluetoothGattService service = new BluetoothGattService(UUID_SERVICE, BluetoothGattService.SERVICE_TYPE_PRIMARY);
//        //添加可读+通知characteristic
//        BluetoothGattCharacteristic characteristicRead = new BluetoothGattCharacteristic(UUID_CHAR_READ_NOTIFY,
//                BluetoothGattCharacteristic.PROPERTY_READ | BluetoothGattCharacteristic.PROPERTY_NOTIFY, BluetoothGattCharacteristic.PERMISSION_READ);
//        characteristicRead.addDescriptor(new BluetoothGattDescriptor(UUID_DESC_NOTITY, BluetoothGattCharacteristic.PERMISSION_WRITE));
//        service.addCharacteristic(characteristicRead);
//        //添加可写characteristic
//        BluetoothGattCharacteristic characteristicWrite = new BluetoothGattCharacteristic(UUID_CHAR_WRITE,
//                BluetoothGattCharacteristic.PROPERTY_WRITE, BluetoothGattCharacteristic.PERMISSION_WRITE);
//        service.addCharacteristic(characteristicWrite);
//        if (bluetoothManager != null)
//            mBluetoothGattServer = bluetoothManager.openGattServer(context, mBluetoothGattServerCallback);
//        mBluetoothGattServer.addService(service);
//    }
//
//    // BLE广播Callback
//    private AdvertiseCallback mAdvertiseCallback = new AdvertiseCallback() {
//        @Override
//        public void onStartSuccess(AdvertiseSettings settingsInEffect) {
//            logTv("BLE广播开启成功");
//        }
//
//        @Override
//        public void onStartFailure(int errorCode) {
//            logTv("BLE广播开启失败,错误码:" + errorCode);
//        }
//
//
//    };
//    private void logTv(String s) {
//        Log.e(TAG,s);
//    }
//    // BLE服务端Callback
//    private BluetoothGattServerCallback mBluetoothGattServerCallback = new BluetoothGattServerCallback() {
//        @Override
//        public void onConnectionStateChange(BluetoothDevice device, int status, int newState) {
//            Log.i(TAG, String.format("onConnectionStateChange:%s,%s,%s,%s", device.getName(), device.getAddress(), status, newState));
//            logTv(String.format(status == 0 ? (newState == 2 ? "与[%s]连接成功" : "与[%s]连接断开") : ("与[%s]连接出错,错误码:" + status), device));
////            if(status == 0 && newState == 2){
////                Message message = new Message();
////                message.what = MainActivity.animeStart;
////                Bundle bundle = new Bundle();
////                bundle.putString("name", device.getName());
////                bundle.putString("address", device.getAddress());
////                message.setData(bundle);
////                MainActivity.animehandler.sendMessageDelayed(message, 1000);
////            }
//
//        }
//
//        @Override
//        public void onServiceAdded(int status, BluetoothGattService service) {
//            Log.i(TAG, String.format("onServiceAdded:%s,%s", status, service.getUuid()));
//            logTv(String.format(status == 0 ? "添加服务[%s]成功" : "添加服务[%s]失败,错误码:" + status, service.getUuid()));
//        }
//
//        @Override
//        public void onCharacteristicReadRequest(BluetoothDevice device, int requestId, int offset, BluetoothGattCharacteristic characteristic) {
//            Log.i(TAG, String.format("onCharacteristicReadRequest:%s,%s,%s,%s,%s", device.getName(), device.getAddress(), requestId, offset, characteristic.getUuid()));
//            String response = "CHAR_" + (int) (Math.random() * 100) + "ma"; //模拟数据
//            mBluetoothGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, offset, response.getBytes());// 响应客户端
//            logTv("客户端读取Characteristic[" + characteristic.getUuid() + "]:\n" + response);
//
//        }
//
//        @Override
//        public void onCharacteristicWriteRequest(BluetoothDevice device, int requestId, BluetoothGattCharacteristic characteristic, boolean preparedWrite, boolean responseNeeded, int offset, byte[] requestBytes) {
//            // 获取客户端发过来的数据
//            String requestStr;
//            requestStr = ByteTool.byte2hex(requestBytes);
//            Log.i(TAG, String.format("onCharacteristicWriteRequest:%s,%s,%s,%s,%s,%s,%s,%s", device.getName(), device.getAddress(), requestId, characteristic.getUuid(),
//                    preparedWrite, responseNeeded, offset, requestStr));
//            mBluetoothGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, offset, requestBytes);// 响应客户端
//            logTv("客户端写入Characteristic[" + characteristic.getUuid() + "]:\n" + requestStr);
//        }
//
//        @Override
//        public void onDescriptorReadRequest(BluetoothDevice device, int requestId, int offset, BluetoothGattDescriptor descriptor) {
//            Log.i(TAG, String.format("onDescriptorReadRequest:%s,%s,%s,%s,%s", device.getName(), device.getAddress(), requestId, offset, descriptor.getUuid()));
//            String response = "DESC_" + (int) (Math.random() * 100); //模拟数据
//            mBluetoothGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, offset, response.getBytes()); // 响应客户端
//            logTv("客户端读取Descriptor[" + descriptor.getUuid() + "]:\n" + response);
//        }
//
//        @Override
//        public void onDescriptorWriteRequest(final BluetoothDevice device, int requestId, BluetoothGattDescriptor descriptor, boolean preparedWrite, boolean responseNeeded, int offset, byte[] value) {
//            // 获取客户端发过来的数据
//            String valueStr = Arrays.toString(value);
//            Log.i(TAG, String.format("onDescriptorWriteRequest:%s,%s,%s,%s,%s,%s,%s,%s", device.getName(), device.getAddress(), requestId, descriptor.getUuid(),
//                    preparedWrite, responseNeeded, offset, valueStr));
//            mBluetoothGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, offset, value);// 响应客户端
//            logTv("客户端写入Descriptor[" + descriptor.getUuid() + "]:\n" + valueStr);
//
//            // 简单模拟通知客户端Characteristic变化
//            if (Arrays.toString(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE).equals(valueStr)) { //是否开启通知
//                final BluetoothGattCharacteristic characteristic = descriptor.getCharacteristic();
//                new Thread(new Runnable() {
//                    @Override
//                    public void run() {
//                        for (int i = 0; i < 5; i++) {
//                            SystemClock.sleep(3000);
//                            String response = "CHAR_" + (int) (Math.random() * 100); //模拟数据
//                            characteristic.setValue(response);
//                            mBluetoothGattServer.notifyCharacteristicChanged(device, characteristic, false);
//                            logTv("通知客户端改变Characteristic[" + characteristic.getUuid() + "]:\n" + response);
//                        }
//                    }
//                }).start();
//            }
//        }
//
//        @Override
//        public void onExecuteWrite(BluetoothDevice device, int requestId, boolean execute) {
//            Log.e(TAG, String.format("onExecuteWrite:%s,%s,%s,%s", device.getName(), device.getAddress(), requestId, execute));
//        }
//
//        @Override
//        public void onNotificationSent(BluetoothDevice device, int status) {
//            Log.e(TAG, String.format("onNotificationSent:%s,%s,%s", device.getName(), device.getAddress(), status));
//        }
//
//        @Override
//        public void onMtuChanged(BluetoothDevice device, int mtu) {
//            Log.e(TAG, String.format("onMtuChanged:%s,%s,%s", device.getName(), device.getAddress(), mtu));
//        }
//    };
//
//    @Override
//    public void sendToRemote(Message.siecomproto msg) {
//        super.sendToRemote(msg);
//
//
//    }
//}
